package com.mingmen.supplier.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mingmen.supplier.R;
import com.mingmen.supplier.model.CaiGouRightModel;
import com.mingmen.supplier.util.GsonUtil;

import java.util.List;

/**
 * Created by yjwfn on 17-9-23.
 */

public class CaiGouZhongAdapter extends RecyclerView.Adapter<CaiGouZhongAdapter.PhotoViewHolder> {


    Context context;
    List<CaiGouRightModel.DetailsBean> dataBeanList;
    String receive_Data;

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position, List<CaiGouRightModel.DetailsBean> dataBeanList, List<CaiGouRightModel.DetailsBean> mNewList);
    }

    private OnitemClick onitemClick;   //定义点击事件接口

    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public CaiGouZhongAdapter(Context context, String receive_Data) {
        this.context = context;
        this.dataBeanList = dataBeanList;

        this.receive_Data = receive_Data;
    }

 /*   public void setDataList(List<CaiGouRightModel.DetailsBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
        notifyDataSetChanged();
    }
*/

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        return new PhotoViewHolder(inflater.inflate(R.layout.item_caigou_caigouzhong, parent, false));
    }


    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
//        holder.photoView.setImageResource(photos[position]);

        int parentposition = position;
        CaiGouRightModel caiGouRightModel = new CaiGouRightModel();

        caiGouRightModel = GsonUtil.GsonToBean(receive_Data, CaiGouRightModel.class);
        dataBeanList = caiGouRightModel.getDetails();
        holder.text_name.setText(dataBeanList.get(position).getSupplierName());
        holder.limitDate.setText(caiGouRightModel.getLimitDate());
        holder.applyID.setText(caiGouRightModel.getBillId());
        holder.timespan.setText(caiGouRightModel.getTimespan());
        holder.supplierName.setText(caiGouRightModel.getSupplierName());
        String status = dataBeanList.get(position).getStatus();
        if (status.equals("4")) {
            holder.status.setText("配送中");
        } else if (status.equals("6")) {
            holder.status.setText("采购从供应商撤回");
        } else if (status.equals("7")) {
            holder.status.setText("供应商退回");
        } else if (status.equals("8")) {
            holder.status.setText("供应商发货");
        } else if (status.equals("9")) {
            holder.status.setText("货到入库");
        } else if (status.equals("10")) {
            holder.status.setText("供应商部分退回");
        }


        if (holder.chuShiRightAdapter == null) {
            holder.chuShiRightAdapter = new CaigouZhongiRightAdapter(context, dataBeanList, position);
            LinearLayoutManager layoutManage = new LinearLayoutManager(context);
            holder.rv_item.setLayoutManager(layoutManage);
            holder.rv_item.setNestedScrollingEnabled(false);
//            holder.rvItemItem.addItemDecoration(new GridSpacingItemDecoration(2, 20, false));
            holder.rv_item.setAdapter(holder.chuShiRightAdapter);
            holder.rv_item.setNestedScrollingEnabled(false);
        } else {
            holder.rv_item.setNestedScrollingEnabled(false);
            holder.rv_item.setNestedScrollingEnabled(false);

            holder.chuShiRightAdapter.setPosition(position);
            holder.chuShiRightAdapter.notifyDataSetChanged();
        }

        holder.chuShiRightAdapter.setOnitemClickLintener(new CaigouZhongiRightAdapter.OnitemClick() {
            @Override
            public void onItemClick(View view, int position, List<CaiGouRightModel.DetailsBean> mList) {
                onitemClick.onItemClick(view, position, dataBeanList, dataBeanList);
            }


        });
        holder.btn_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position, dataBeanList, dataBeanList);
            }
        });
        holder.btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position, dataBeanList, dataBeanList);
            }
        });
        holder.btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position, dataBeanList, dataBeanList);
            }
        });
    }

    @Override
    public int getItemCount() {
//        return dataBeanList.size();
        return 1;
    }

    static final class PhotoViewHolder extends RecyclerView.ViewHolder {


        TextView text_name;
        RecyclerView rv_item;
        CaigouZhongiRightAdapter chuShiRightAdapter;
        TextView btn_detail;
        TextView btn_save;
        TextView btn_submit;
        TextView limitDate;
        TextView applyID;
        TextView timespan;
        TextView supplierName;
        TextView status;

        public PhotoViewHolder(View itemView) {
            super(itemView);

            text_name = itemView.findViewById(R.id.text_name);
            rv_item = itemView.findViewById(R.id.rv_item);
            btn_detail = itemView.findViewById(R.id.btn_detail);
            btn_save = itemView.findViewById(R.id.btn_save);
            btn_submit = itemView.findViewById(R.id.btn_submit);
            limitDate = itemView.findViewById(R.id.limitDate);
            applyID = itemView.findViewById(R.id.applyID);
            timespan = itemView.findViewById(R.id.timespan);
            supplierName = itemView.findViewById(R.id.supplierName);
            status = itemView.findViewById(R.id.status);
        }
    }
}
